<?PHP //$Id: block_matriculation.php,v 1.0 2009/08/11 08:50:00 moodler Exp $

global $CFG;
require_once ("$CFG->dirroot/blocks/matriculation/lib.php");

class block_matriculation extends block_base {
	
    function init() {
        $this->title = get_string('blockname', 'block_matriculation');
        $this->version = 2007101509; 
    }

    function get_content() {
	
      	global $CFG; 
		global $COURSE;  
		$users=array();
		
        if ( $this->content !== NULL ) {
            return  $this->content;
        }
		
		if (!empty($this->instance->pageid)) {
            $context = get_context_instance(CONTEXT_COURSE, $this->instance->pageid);
            if ($COURSE->id == $this->instance->pageid) {
                $course = $COURSE;
            } else {
                $course = get_record('course', 'id', $this->instance->pageid);
            }
        } else {
            $context = get_context_instance(CONTEXT_SYSTEM);
            $course = $SITE;
        }
		
		
		if (!has_capability('block/matriculation:view', $context)) {  // Just return
            return $this->content;
        }
		
		
		$this->content = new stdClass;
		$this->content->text .= '<div style="text-align:center"><a href='.$CFG->wwwroot.'/blocks/matriculation/view.php?action=5>'.get_string('access', 'block_matriculation').'</a></div>';
		
		$filename="$CFG->dirroot/enrol/imsenterprise/matriculation/datos.csv";
		
		$error=block_matriculation_read_file($filename, $users);
		
		switch ($error) {
			case 0: $results=block_matriculation_write_db($users);
					if ($results) 
						$this->content->text .= get_string('file_error', 'block_matriculation').'<br>';		
					else 
						$this->content->text .= get_string('upload_file', 'block_matriculation').'<br>';
					break;
			
			case 1: $this->content->text .=get_string('file_not_exist', 'block_matriculation').'<br>';
					break;
					
			case 2: $this->content->text .=get_string('file_not_open', 'block_matriculation').'<br>';
					break;
					
			case 3: $this->content->text .=get_string('not_upload_file', 'block_matriculation').'<br>';
					break;
					
			case 4: $this->content->text .=get_string('file_empty', 'block_matriculation').'<br>';
					break;
					
		}
		
		
		if (count_records('block_matriculation_temp')>0) { // existen datos en la tabla temporal
		
			$this->content->text .= '<br>'.get_string('exist_data', 'block_matriculation');
			$results_exist=block_matriculation_compare_db_count(1);
			$results_notexist=block_matriculation_compare_db_count(2);
			
			if ($results_exist==0 && $results_notexist==0) {
				delete_records('block_matriculation_temp');
				$this->content->text=get_string('same_data', 'block_matriculation');
			} else {	
				
				if ($results_exist >0) { // ALTAS
					$this->content->text .= '<br><a href='.$CFG->wwwroot.'/blocks/matriculation/view.php?action=1>'.get_string('manage_add', 'block_matriculation').' ('.$results_exist.')</a>';
				}
				
				if ($results_notexist > 0) {  // BAJAS O CONVALIDACIONES
					$this->content->text .= '<br><a href='.$CFG->wwwroot.'/blocks/matriculation/view.php?action=2>'.get_string('manage_remove', 'block_matriculation').'('.$results_notexist.')</a>';
				}
			}
		}
		else 
			$this->content->text .= get_string('no_data', 'block_matriculation');
		
        $this->content->footer = '';
        return $this->content;
    }

}

?>
